package com.movesti.android.app.quickcontact.component;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.movester.quickcontact.R;
import com.movesti.android.app.quickcontact.activity.CallLogActivity;
import com.movesti.android.app.quickcontact.setting.SettingManager;
import com.movesti.android.app.quickcontact.util.PhoneUtil;

public class CallLogListAdapter extends CursorAdapter implements OnClickListener {

    public CallLogListAdapter(Context context, Cursor c) {
        super(context, c, true);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.calllog_call_entry, parent, false);
        ViewCache vc = new ViewCache();
        vc.name = (TextView) v.findViewById(R.id.name);
        vc.date = (TextView) v.findViewById(R.id.date);
        vc.callIcon = (ImageView) v.findViewById(R.id.call_icon);
        vc.callTypeIcon = (ImageView) v.findViewById(R.id.call_type_icon);
        vc.callIcon.setOnClickListener(this);
        v.setTag(vc);
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewCache vc = (ViewCache) view.getTag();
        long date = cursor.getLong(CallLogActivity.ColumnIndex.Date);
        String cachedName = cursor.getString(CallLogActivity.ColumnIndex.Cached_Name);
        String number = cursor.getString(CallLogActivity.ColumnIndex.Number);
        int type = cursor.getInt(CallLogActivity.ColumnIndex.Type);
        vc.contactId = cursor.getLong(CallLogActivity.ColumnIndex.Id);
        vc.date.setText(DateUtils.getRelativeTimeSpanString(date, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE));
        vc.cachedNumber = number;
        if (cachedName == null) {
            vc.name.setText(number);
        } else {
            vc.name.setText(cachedName);
        }
        int textSize = SettingManager.getInstance(context).GlobalSetting.getTextSize();
        if (vc.name.getTextSize() != textSize) {
            vc.name.setTextSize(textSize);
        }
        if (type == CallLog.Calls.INCOMING_TYPE) {
            vc.callTypeIcon.setBackgroundResource(R.drawable.calllog_icon_incoming);
        } else if (type == CallLog.Calls.OUTGOING_TYPE) {
            vc.callTypeIcon.setBackgroundResource(R.drawable.calllog_icon_outgoing);
        } else if (type == CallLog.Calls.MISSED_TYPE) {
            vc.callTypeIcon.setBackgroundResource(R.drawable.calllog_icon_missed);
        }
        view.requestLayout();
    }

    public final class ViewCache {
        public ImageView callIcon, callTypeIcon;
        public TextView date, name;
        public long contactId;
        public String cachedNumber;
    }

    @Override
    public void onClick(View v) {
        // clicked on the call icon, dial the cached number
        PhoneUtil.callNumber(((ViewCache) ((View) v.getParent()).getTag()).cachedNumber, v.getContext());
    }
}
